package com.whj.wkpt.controller;

import com.whj.wkpt.dto.PageResultDTO;
import com.whj.wkpt.entity.Task;
import com.whj.wkpt.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class TaskController {

    @Autowired
    private TaskService taskService;

    @GetMapping("/tasks")
    public String listTasks(
            @RequestParam(required = false) String title,
            @RequestParam(required = false) String taskStatus,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "6") int size,
            Model model) {
        
        // 调用服务层进行分页查询
        PageResultDTO<Task> pageResult = taskService.findTasksWithPagination(title, taskStatus, page, size);
        
        model.addAttribute("tasks", pageResult.getContent());
        model.addAttribute("currentPage", pageResult.getPage());
        model.addAttribute("totalPages", pageResult.getTotalPages());
        model.addAttribute("totalElements", pageResult.getTotalElements());
        model.addAttribute("title", title);
        model.addAttribute("taskStatus", taskStatus);
        
        return "task-list";
    }
}